import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import QtQuick.Controls.Styles 1.4
import QtQuick.Controls 2.3 as Ctrl2
import LoginService 1.0
import QtQuick.Dialogs 1.3

Item {
    signal login()

    anchors.fill:parent
    MessageDialog {
        id: messageDialog
        standardButtons: StandardButton.Ok
        title: "yiqi"
        text: "The document has been modified."
    }

    LoginService{
        id:loginService
    }
    ColumnLayout{
        anchors.fill:parent
        Rectangle{
            Layout.fillWidth: true
            Layout.preferredHeight: 242
            BorderImage {
                id: name
                source: "qrc:/img/login_title.jpg"
                anchors.fill:parent
            }
        }
        GridLayout{
            Layout.leftMargin: 20
            Layout.topMargin: 20
            Layout.preferredHeight: 123
            Layout.alignment: Qt.AlignVCenter
            columns: 2
            rows: 3

            Image{
                source:"qrc:/img/username.png"
                sourceSize: Qt.size(34,34)
                Layout.preferredHeight: 34
                Layout.preferredWidth: 34
            }

            TextField {
                id: username
                text: ""
                placeholderText: "请输入用户名"
                horizontalAlignment: Text.AlignLeft
                Layout.fillWidth: true
                Layout.preferredHeight: 34
                font.pixelSize: 14
                style:textFieldStyle
            }

            Image{
                source:"qrc:/img/password.png"
                sourceSize: Qt.size(34,34)
                Layout.preferredHeight: 34
                Layout.preferredWidth: 34
            }

            TextField {
                id: password
                text: qsTr("")
                placeholderText: "请输入密码"
                echoMode: TextInput.Password
                font.pixelSize: 14
                Layout.fillWidth: true
                Layout.preferredHeight: 34
                style:textFieldStyle
            }

        }

        RowLayout{
            Layout.leftMargin: 20
            Layout.topMargin: 20
            Ctrl2.Button {
                id:loginBtn
                text: "登录"
                contentItem: Text {
                    text: loginBtn.text
                    font: loginBtn.font
                    opacity: enabled ? 1.0 : 0.3
                    color:"#ffffff"
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                }
                background: Rectangle {
                    implicitWidth: 157
                    implicitHeight: 40
                    opacity: enabled ? 1 : 0.3
                    color:loginBtn.down?"#31b0d5":"#2a9cff"
                    radius: 2
                }
            }
            Ctrl2.Button {
                id:configBtn
                text: "设置"
                contentItem: Text {
                    text: configBtn.text
                    font: configBtn.font
                    opacity: enabled ? 1.0 : 0.3
                    color:"#ffffff"
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                }
                background: Rectangle {
                    implicitWidth: 157
                    implicitHeight: 40
                    opacity: enabled ? 1 : 0.3
                    color:configBtn.down?"#31b0d5":"#2a9cff"
                    radius: 2
                }
            }
        }


        Rectangle{
            Layout.fillWidth: true
            Layout.fillHeight: true
        }
    }
    Component{
        id:textFieldStyle
        TextFieldStyle{
            background:Rectangle{
                color:"transparent"
                Rectangle{
                    width:parent.width
                    height:1
                    color:"#eeeeee"
                    anchors{
                        bottom:parent.bottom
                        bottomMargin: 0
                    }
                }
            }
            placeholderTextColor:"#c7c7c7"
        }
    }
    Component.onCompleted: {
        if(settings.value("user/pid",null)!==null){
            login()
        }
    }

    Connections{
        target:loginBtn
        onClicked:{
            loginService.username = username.text
            loginService.password = password.text
            loginService.slot_login_req()
        }
    }

    Connections{
        target:loginService
        onSig_login_result:{
            messageDialog.text = msg
            messageDialog.visible = true
            if(code == 1)
                login()
        }

    }
}
